home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 114 / macaddict114.cdr / Software / Utilities / macam.0.8.4.dmg / macam sources / utilities / MiscTools.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-12-30  |  4.8 KB  |  156 lines

  1. /*
  2.  macam - webcam app and QuickTime driver component
  3.  Copyright (C) 2002 Matthias Krauss (macam@matthias-krauss.de)
  4.  
  5.  This program is free software; you can redistribute it and/or modify
  6.  it under the terms of the GNU General Public License as published by
  7.  the Free Software Foundation; either version 2 of the License, or
  8.  (at your option) any later version.
  9.  
  10.  This program is distributed in the hope that it will be useful,
  11.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  GNU General Public License for more details.
  14.  
  15.  You should have received a copy of the GNU General Public License
  16.  along with this program; if not, write to the Free Software
  17.  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  $Id: MiscTools.c,v 1.2 2002/12/30 17:50:58 mattik Exp $
  19.  */
  20.  
  21. #include "MiscTools.h"
  22. #include "pthread.h"
  23. #include "sched.h"
  24. #include "Resolvers.h"
  25.  
  26. void CStr2PStr(const char* cstr, unsigned char* pstr) {
  27.     short i=0;
  28.     for (i=0;cstr[i]!=0;i++)  pstr[i+1]=cstr[i];
  29.     pstr[0]=i;
  30. }
  31.  
  32. void PStr2CStr(const unsigned char* pstr, char* cstr) {
  33.     short i=0;
  34.     for (i=0;i<pstr[0];i++)  cstr[i]=pstr[i+1];
  35.     cstr[i]=0;
  36. }
  37.  
  38. void PStr2PStr(const unsigned char* src, unsigned char* dst) {
  39.     short i;
  40.     for (i=0;i<=src[0];i++) dst[i]=src[i];
  41. }
  42.  
  43. void CStr2CStr(const char* src,char* dst) {
  44.     while (*src) *(dst++)=*(src++);
  45.     *dst=0;
  46. }
  47.  
  48. void DumpMem(unsigned char* buf, long len) {
  49.     long i;
  50.     for (i=0;i<len;i++) {
  51.         printf("%02x ",buf[i]);
  52.         if (((i%16)==15)||(i==len-1)) printf("\n");
  53.     }
  54. }
  55.  
  56. /*I have no idea about pthread scheduling and priorities, but this seems to work and seemed to be the most logical to me when I looked at the headers. Anyone with expertise? */
  57.  
  58. void ChangeMyThreadPriority(int delta) {
  59.     pthread_t thread=pthread_self();
  60.     struct sched_param param;
  61.     int policy;
  62.     if (pthread_getschedparam(thread,&policy,¶m)==0) {
  63.         param.sched_priority+=delta;
  64.         pthread_setschedparam(thread,policy,¶m);
  65.     }    
  66. }
  67.  
  68. int GetMyThreadPriority(void) {
  69.     pthread_t thread=pthread_self();
  70.     struct sched_param param;
  71.     int policy;
  72.     if (pthread_getschedparam(thread,&policy,¶m)==0) return param.sched_priority;
  73.     else return 0;
  74. }
  75.  
  76. void OSXYieldToAnyThread(void) {
  77.     sched_yield();
  78. }
  79.  
  80. short CountPipes(IOUSBInterfaceInterface **intf) {
  81.     UInt8 count;
  82.     IOReturn err;
  83.     if (!intf) return 0;
  84.     err=(*intf)->GetNumEndpoints(intf,&count);
  85.     if (err) printf("CountPipes: Error %d\n",err);
  86.     return count;
  87. }
  88.  
  89. void ShowPipeInfo(IOUSBInterfaceInterface **intf, short idx) {
  90.     IOReturn                err;
  91.     UInt8                direction;
  92.     UInt8                number;
  93.     UInt8                transferType;
  94.     UInt16                maxPacketSize;
  95.     UInt8                interval;
  96.  
  97.     err= (*intf)->GetPipeProperties(intf,idx,&direction,&number, &transferType,&maxPacketSize,&interval);
  98.     CheckError(err,"GetPipeProperties");
  99.     printf("Pipe %d: dir:",idx);
  100.     switch (direction) {
  101.         case kUSBOut: printf("out"); break;
  102.         case kUSBIn: printf("in"); break;
  103.         case kUSBAnyDirn: printf("bidirectional"); break;
  104.         default: printf("invalid"); break;
  105.     }
  106.     printf (" number:%d type:",number);
  107.     switch (transferType) {
  108.         case kUSBControl: printf("control"); break;
  109.         case kUSBIsoc: printf("isochronous"); break;
  110.         case kUSBBulk: printf("bulk"); break;
  111.         case kUSBInterrupt: printf("interrupt"); break;
  112.         case kUSBAnyType: printf("any type"); break;
  113.         default: printf("invalid"); break;
  114.     }
  115.     printf(" maxPacketSize:%d pollInterval:%d ",maxPacketSize,interval);
  116.     err=(*intf)->GetPipeStatus(intf,idx);
  117.     ShowError(err,"Status");
  118. }
  119.  
  120. void ShowPipesInfo(IOUSBInterfaceInterface **intf) {
  121.     short num=CountPipes(intf);
  122.     short i;
  123.     printf("Number Of Pipes: %d (+ default control pipe 0)\n",num);
  124.     for (i=0;i<=num;i++) ShowPipeInfo(intf,i);
  125. }
  126.  
  127. short WidthOfResolution(CameraResolution r) {
  128.     short ret;
  129.     switch (r) {
  130.         case ResolutionSQSIF: ret = 128; break;
  131.         case ResolutionQSIF:  ret = 160; break;
  132.         case ResolutionQCIF:  ret = 176; break;
  133.         case ResolutionSIF:   ret = 320; break;
  134.         case ResolutionCIF:   ret = 352; break;
  135.         case ResolutionVGA:   ret = 640; break;
  136.         case ResolutionSVGA:  ret = 800; break;
  137.         default:              ret =  -1; break;
  138.     }
  139.     return ret;
  140. }
  141.  
  142. short HeightOfResolution(CameraResolution r) {
  143.     short ret;
  144.     switch (r) {
  145.         case ResolutionSQSIF: ret =  96; break;
  146.         case ResolutionQSIF:  ret = 120; break;
  147.         case ResolutionQCIF:  ret = 144; break;
  148.         case ResolutionSIF:   ret = 240; break;
  149.         case ResolutionCIF:   ret = 288; break;
  150.         case ResolutionVGA:   ret = 480; break;
  151.         case ResolutionSVGA:  ret = 600; break;
  152.         default:              ret =  -1; break;
  153.     }
  154.     return ret;
  155. }
  156.